package client.components;

import client.PlanModel;
import client.bean.Plan;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;

public class NewPlanDialogBox extends DialogBox {

	private TextBox nameField = new TextBox();
	private TextArea descriptionField = new TextArea();
	private DateBox planStartDateField = new DateBox();
	private DateBox planEndDateField = new DateBox();

	public NewPlanDialogBox() {
		setModal(true);
		setGlassEnabled(true);

		VerticalPanel vp = new VerticalPanel();

		HorizontalPanel hp0 = new HorizontalPanel();
		hp0.add(new Label("Name:"));
		nameField.setText("New Plan name");
		hp0.add(nameField);

		HorizontalPanel hp1 = new HorizontalPanel();
		hp1.add(new Label("Description:"));
		descriptionField.setText("THe Plan Description");
		hp1.add(descriptionField);

		HorizontalPanel hp2 = new HorizontalPanel();
		hp2.add(new Label("Start date:"));
		hp2.add(planStartDateField);

		HorizontalPanel hp3 = new HorizontalPanel();
		hp3.add(new Label("End date:"));
		hp3.add(planEndDateField);

		Button okButton = new Button("Ok");
		okButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fillModel();
				hide();
			}
		});

		Button cancelButton = new Button("Cancel");
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide();
			}
		});

		vp.add(hp0);
		vp.add(hp1);
		vp.add(hp2);
		vp.add(hp3);

		HorizontalPanel buttonPanel = new HorizontalPanel();
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		vp.add(buttonPanel);

		setWidget(vp);

		center();
	}

	private void fillModel() {
		PlanModel model = PlanModel.instance;
		Plan plan = new Plan();
		plan.setId(model.getLatestPlanId() + 1);
		plan.setName(nameField.getValue());
		plan.setDescription(descriptionField.getValue());
		plan.setStartTime(planStartDateField.getValue());
		plan.setEndTime(planEndDateField.getValue());
		model.addPlan(plan);
	}
}
